import { useGlobalContent } from "@/store";
// import { Boot } from "@wangeditor/editor";
// import attachmentModule from "@wangeditor/plugin-upload-attachment";
import { getRowByField, TipsModal, useCommonMessage } from "hy_dev_lib";
import { useEffect } from "react";
import { Navigate, Route, Routes, useLocation, useNavigate } from "react-router-dom";
import "./app.less";
import { getTokenCookies, logout } from "./common/auth/token";
import ReasonModal from "./gcfx_web/components/ReasonModal";
import ProDetailPage from "./gcfx_web/pages/ProDetailPage";
import useModal from "./hy_dev_lib/src/components/useModal";
import { routerInfo } from "./routes";
import ErrorPage from "./views/404/Error";
import Home from "./views/Home";
import Login from "./views/Login";
import { useGetMenu } from "./views/Login/useGetMenu";

/** app 入口 */
function App() {
  const { globalDispatch, global } = useGlobalContent();
  const navigate = useNavigate();
  const location = useLocation();
  const pathname = location.pathname;
  const { MENU_LIST: menuList, systemInfo, } = global || {};
  const { contextHolder } = useCommonMessage()
  useModal();

  // 为富文本编辑器注册上传附件插件
  useEffect(() => {
    // 没有注册过才可以注册
    // if (Boot?.plugins?.length < 13) Boot.registerModule(attachmentModule);
  }, []);

  // 没有token时跳转登陆页
  useEffect(() => {
    checkToken();
  }, [pathname]);

  const { getMenuFn } = useGetMenu(globalDispatch);

  //初始化菜单数据
  const initMenuData = async () => {
    getMenuFn();
  };

  useEffect(() => {
    initSystemInfo();
    if (checkToken()) return;
    if (!global.MENU_LIST?.length) initMenuData();
  }, []);

  // 处理没有权限的路由
  const haneleNoPowerPath = () => {
    // 当前访问的地址是否在路由中
    const pathInRoute = getRowByField(routerInfo || [], pathname.slice(5), "path");
    // 当前访问的地址是否在菜单列表中
    const pathInMenu = getRowByField(menuList || [], pathname.slice(5), "cdlj");
    // 不在菜单列表中 但有该路由的 退出登录
    if (pathInRoute && !pathInMenu) logout();
  };

  // 无菜单判断+设置默认菜单
  useEffect(() => {
    if (pathname == "/login") return;
    if (menuList && menuList?.length === 0) return navigate("/home/noPermission");
    if (menuList && menuList?.length > 0 && pathname === "/home/noPermission") return navigate("/home" + defaultRoute);
    if (menuList?.length) haneleNoPowerPath();
  }, [menuList, pathname]);

  /** return true 中断后续代码 */
  const checkToken = () => {
    if (pathname == "/login") return true;
    if (pathname.includes("wecom")) return true;
    if (!getTokenCookies()) {
      navigate("/login");
      return true;
    }
  };

  // 获取并设置配置的系统信息
  const initSystemInfo = async () => {
    // const [data] = await (getSystemInfo() || {});
    const data = {} as any
    globalDispatch({
      type: "systemInfo",
      data: data || {},
    });
    // TODO: 设置浏览器相关信息
    const html_title = document.querySelector("#html_title");
    if (data.xtmc && html_title) html_title.innerHTML = data.xtmc;
    const html_logo = document.querySelector("#html_logo") as HTMLLinkElement;
    if (data.llqlgdz && html_logo) html_logo.href = SYSTEM_GLOBAL.fileBaseURL + data.llqlgdz;
  };

  // const menuList = global.MENU_LIST;
  const getDefaultRoute = () => {
    if (!menuList) return null;
    return menuList[0]?.children[0]?.cdlj || menuList[0]?.cdlj || "/";
  };

  const defaultRoute = getDefaultRoute();

  return (
    <div className={`app`}>
      <Routes>
        {defaultRoute && <Route path="/" element={<Navigate replace to={"/home" + defaultRoute} />}></Route>}
        <Route path="/login" element={<Login />}></Route>
        {/* 主页下的页面需要在Home中注册路由 */}
        <Route path="/home/*" element={<Home menuList={menuList} initMenuData={initMenuData} />}></Route>
        <Route path="/404" element={<ErrorPage />}></Route>
        {menuList && <Route path="*" element={<Navigate replace to="/404" />} />}
      </Routes>
      {contextHolder}
      <TipsModal></TipsModal>
      <ReasonModal></ReasonModal>
      <ProDetailPage></ProDetailPage>
    </div>
  );
}

export default App;
